<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Password extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {

    }

    function forget() {
        $data = array();
        $this->load->helper('captcha');
        $act = $this->input->post('act');
        if ($act == 'mail') {
            $ret = array('msg' => '');
            $captcha = $this->input->post('captcha');
            $email = $this->input->post('email');
            $username = $this->input->post('username');
            if ($email == '') {
                $ret['msg'] = '请填写邮箱';
            } elseif ($username == '') {
                $ret['msg'] = '请填写用户名';
            } elseif ($captcha == "") {
                $ret['msg'] = '验证码不能为空';
            } elseif ($captcha != $_SESSION['captcha']) {
                $ret['msg'] = '验证码不正确';
            }
            if ($ret['msg'] != '') {
                echo json_encode($ret);
                exit;
            }

            $this->load->model('User_model', 'user');
            $where = array(
                'username' => $username,
                'email' => $email
            );
            $userinfo = $this->user->get_one($where, 'user_id');
            if (!$userinfo) {
                $ret['msg'] = '邮箱和用户名不匹配';
                echo json_encode($ret);
                exit;
            }

            $res = $this->Notice(2, $userinfo['user_id']);
            if ($res) {
                //$this->js->alert('邮件发送成功，请从邮件内容里的链接地址重新设置交易密码，该链接有效时间1小时，请及时操作！');
                $ret['msg'] = '邮件发送成功，请从邮件内容里的链接地址重置密码，该链接有效时间1小时，请及时操作！';
            } else {
                //$this->js->alert('邮箱发送失败，请联系客服！');
                $ret['msg'] = '邮箱发送失败，请稍后再试或者联系客服！';
            }
            echo json_encode($ret);
            exit;
        } elseif($act == 'phone') {
            $ret = array('msg' => '');
            $captcha = $this->input->post('captcha');
            $phone = $this->input->post('phone');
            $username = $this->input->post('username');
            $new_password = $this->input->post('new_password');
            if ($phone == '') {
                $ret['msg'] = '请填写手机号';
            } elseif ($username == '') {
                $ret['msg'] = '请填写用户名';
            } elseif ($captcha == "") {
                $ret['msg'] = '验证码不能为空';
            } elseif ($captcha != $this->session->userdata('zjy_temp_captcha')) {
                $ret['msg'] = '验证码不正确';
            }
            if ($ret['msg'] != '') {
                echo json_encode($ret);
                exit;
            }

            $this->load->model('User_model', 'user');
            $where = array(
                'username' => $username,
                'phone' => $phone
            );
            $userinfo = $this->user->get_one($where, 'user_id');//userinfo为用户id
            if (!$userinfo) {
                $ret['msg'] = '手机和用户名不匹配';
                echo json_encode($ret);
                exit;
            }
            $res = $this->user->update(array('password'=>md5(md5($new_password))),array('user_id'=>$userinfo['user_id']));
            if ($res) {
                //$this->js->alert('邮件发送成功，请从邮件内容里的链接地址重新设置交易密码，该链接有效时间1小时，请及时操作！');
                $ret['msg'] = '手机修改密码成功！3秒后跳转到登录页';
                $ret['skip'] = 1;
            } else {
                //$this->js->alert('邮箱发送失败，请联系客服！');
                $ret['msg'] = '手机修改密码失败，请联系客服！';
            }
            echo json_encode($ret);
            exit;
        }

        $data['title'] = '忘记密码——' . CON_PLATFORMNAME;
        $this->load->view('user/user_pwd_forget', $data);
    }

    function getpwd() {
        $data = array('msg' => '');
        $act = $this->input->post('act');
        $this->load->library('func');
        $str = $this->input->get('check');
        if (!$str) {
            $data['msg'] = '无效链接';
        } else {
            $str = $this->func->authcode($str, "DECODE");
            $tmp = explode(',', $str);
            //echo json_encode($tmp);exit;
            if (count($tmp) == 4) {
                if (time() - $tmp[0] > 3600) {
                    $data['msg'] = '无效链接';
                } else {
                    $this->load->model('User_model', 'user');
                    $tmp[1] = intval($tmp[1]);
                    $userInfo = $this->user->get_by_uid($tmp[1]);
                    if ($userInfo && strtolower($userInfo['username']) == strtolower($tmp[2]) && strtolower($userInfo['email']) == strtolower($tmp[3])) {
                        $data['user'] = $userInfo;
                        if ($act == 'set') {
                            $ret = array('msg' => '', 'url' => '');
                            $pwd1 = $this->input->post('pwd1');
                            $pwd2 = $this->input->post('pwd2');
                            if (!isset($pwd1[5])) {
                                $ret['msg'] = '密码长度不够';
                                echo json_encode($ret);
                                exit;
                            } elseif ($pwd1 != $pwd2) {
                                $ret['msg'] = '两次密码不一致';
                                echo json_encode($ret);
                                exit;
                            } else {
                                if ($userInfo['password'] == md5(md5($pwd1))) {
                                    $ret['url'] = '/login/index/index.html';
                                    echo json_encode($ret);
                                    exit;
                                }
                                $setArr = array('password' => md5(md5($pwd1)));
                                $res = $this->user->update($setArr, array('user_id' => $tmp[1]));
                                if ($res) {
                                    $ret['url'] = '/login/index/index.html';
                                    echo json_encode($ret);
                                    exit;
                                } else {
                                    $ret['msg'] = '操作失败';
                                    echo json_encode($ret);
                                    exit;
                                }
                            }
                        }
                    } else {
                        $data['msg'] = '无效链接';
                    }
                }
            } else {
                $data['msg'] = '无效链接';
            }
        }

        $data['title'] = "登录密码" . ' - ' . CON_KEYWORDS;
        $this->load->view('user/user_getpwd', $data);
    }

    function paypwd() {
        //$str = implode(',',array($user['user_id'],$user['username'],$user['email'],time()));
        //$str = urlencode($this->func->authcode($str,"ENCODE",'',3600));

        $data = array('msg' => '', 'url' => '');
        $act = $this->input->post('act');
        $this->load->library('func');
        $str = $this->input->get('check');
        $this->load->model('User_model', 'user');
        if (!$str) {
            $data['msg'] = '无效链接';
            $data['url'] = '/user/center/index.html';
        } else {
            $str = $this->func->authcode($str, "DECODE");
            $tmp = explode(',', $str);
            if (count($tmp) == 4) {
                if (time() - $tmp[3] > 3600) {
                    $data['msg'] = '无效链接';
                    $data['url'] = '/user/center/index.html';
                } else {
                    $tmp[0] = intval($tmp[0]);
                    $row = $this->user->get_by_uid($tmp[0]);
                    if ($row && $row['username'] == $tmp[1] && $row['email'] == $tmp[2]) {
                        $data['user'] = $row;
                        if ($act == 'set') {
                            $ret = array('msg' => '', 'url' => '');
                            $pwd1 = $this->input->post('paypwd1');
                            $pwd2 = $this->input->post('paypwd2');
                            if (!isset($pwd1[5])) {
                                $ret['msg'] = '密码长度不够';
                                echo json_encode($ret);
                                exit;
                            } elseif ($pwd1 != $pwd2) {
                                $ret['msg'] = '两次密码不一致';
                                echo json_encode($ret);
                                exit;
                            } else {

                                if ($row['paypassword'] == md5(md5($pwd1))) {
                                    $ret['url'] = '/user/center/index.html';
                                    echo json_encode($ret);
                                    exit;
                                }
                                $setArr = array('paypassword' => md5(md5($pwd1)));
                                $res = $this->user->update($setArr, array('user_id' => $tmp[0]));
                                if ($res) {
                                    $ret['url'] = '/user/center/index.html';
                                    echo json_encode($ret);
                                    exit;
                                } else {
                                    $ret['msg'] = '操作失败';
                                    echo json_encode($ret);
                                    exit;
                                }
                            }
                        }
                    } else {
                        $data['msg'] = '无效链接';
                        $data['url'] = '/user/center/index.html';
                    }
                }
            } else {
                $data['msg'] = '无效链接';
                $data['url'] = '/user/center/index.html';
            }
        }
        if ($act == 'set') {
            $user = $this->session->userdata('zjy_user');
            $user_id = $user['user_id'];
            $ret = array('msg' => '', 'url' => '');
            $pwd1 = $this->input->post('paypwd1');
            $pwd2 = $this->input->post('paypwd2');
            if (!isset($pwd1[5])) {
                $ret['msg'] = '密码长度不够';
                echo json_encode($ret);
                exit;
            } elseif ($pwd1 != $pwd2) {
                $ret['msg'] = '两次密码不一致';
                echo json_encode($ret);
                exit;
            } else {
                $setArr = array('paypassword' => md5(md5($pwd1)));
                $res = $this->user->update($setArr, array('user_id' => $user_id));
                if ($res) {
                    $ret['url'] = '/user/center/index.html';
                    echo json_encode($ret);
                    exit;
                } else {
                    $ret['msg'] = '操作失败';
                    echo json_encode($ret);
                    exit;
                }
            }
        } else {
            $data['title'] = "交易密码" . ' - ' . CON_KEYWORDS;
            $this->load->view('user/user_getpay', $data);
        }
    }

    function phone_paypwd(){
        if(!$this->session->userdata('phone_set_pwd')){
            header("location:/index.php/user/center");
            exit;
        }
        $this->load->model('User_model', 'user');
        $act = $this->input->post('act');
        if ($act == 'set') {
            $user = $this->session->userdata('zjy_user');
            $user_id = $user['user_id'];
            $ret = array('msg' => '', 'url' => '');
            $pwd1 = $this->input->post('paypwd1');
            $pwd2 = $this->input->post('paypwd2');
            if (!isset($pwd1[5])) {
                $ret['msg'] = '密码长度不够';
                echo json_encode($ret);
                exit;
            } elseif ($pwd1 != $pwd2) {
                $ret['msg'] = '两次密码不一致';
                echo json_encode($ret);
                exit;
            } else {
                $setArr = array('paypassword' => md5(md5($pwd1)));
                $res = $this->user->update($setArr, array('user_id' => $user_id));
                if ($res) {
                    $ret['url'] = '/user/center/index.html';
                    echo json_encode($ret);
                    exit;
                } else {
                    $this->session->set_userdata('phone_set_pwd','');
                    $ret['msg'] = '操作失败';
                    echo json_encode($ret);
                    exit;
                }
            }
        }else {
            $data['title'] = "交易密码" . ' - ' . CON_KEYWORDS;
            $this->load->view('user/user_getpay', $data);
        }
    }

}
